home *** CD-ROM | disk | FTP | other *** search
- // -------------------------------------------------------------------------------------
- // ExpandedView.cxx
- // -------------------------------------------------------------------------------------
- // Permission is granted to freely redistribute this source code, and to use fragments
- // of this code in your own applications if you find them to be useful. This class,
- // along with the source code, come with no warranty of any kind, and the user assumes
- // all responsibility for its use.
- // -------------------------------------------------------------------------------------
-
- extern "Objective-C" {
- #import <objc/objc.h>
- #import <appkit/appkit.h>
- #import <libc.h>
- #import <stdlib.h>
- #import <stdio.h>
- #import <string.h>
- #import <math.h>
- #import <sys/time.h>
- #import <defaults/defaults.h>
- #import <dpsclient/psops.h>
- #import <dpsclient/dpsNeXT.h>
- }
-
- #import "ExpandedView.h"
-
- // -------------------------------------------------------------------------------------
- // misc defines
- #define isSHIFT(S) ((([NXApp currentEvent]->flags) & (S))? YES : NO)
- #define X origin.x
- #define Y origin.y
- #define W size.width
- #define H size.height
-
- // -------------------------------------------------------------------------------------
- // PasteBoard dragging operation variables
- extern NXAtom pbTypes[];
- extern int pbNumTypes;
- extern int pbChangeCount;
-
- // -------------------------------------------------------------------------------------
- @implementation ExpandedView
-
- // -------------------------------------------------------------------------------------
- // create a new instance
-
- /* standard init */
- - initFrame:(const NXRect *)frameRect
- {
- [super initFrame:frameRect];
- imageId = (id)nil;
- return self;
- }
-
- // -------------------------------------------------------------------------------------
- // image methods
-
- /* set image */
- - setImage:image :(const char*)fileName
- {
- NXRect vFrame;
-
- /* check for valid image representation */
- imageId = (id)nil;
- imageFile = (char*)nil;
- if (!image) return (id)nil;
- if (![[image representationList] count]) return (id)nil;
- imageId = image;
- imageFile = fileName;
-
- /* resize view to match image */
- [imageId getSize:&(vFrame.size)];
- [self sizeTo:vFrame.W :vFrame.H];
- drawWithAlpha = YES;
- backgroundGray = NX_LTGRAY;
- imageRep = -1;
-
- /* find best rep */
- bestRep = [[image representationList] indexOf:[imageId bestRepresentation]];
-
- return self;
- }
-
- /* return image */
- - image
- {
- return imageId;
- }
-
- /* return alpha status */
- - (BOOL)imageRepHasAlpha:(int)repNum
- {
- if (!imageId || (repNum < 0)) return NO;
- return [[[imageId representationList] objectAt:repNum] hasAlpha];
- }
-
- - (int)bestRepIndex
- {
- return bestRep;
- }
-
- /* set which imageRep to display */
- - setImageRep:(int)repNum withAlpha:(BOOL)useAlpha
- {
- if (!imageId) return (id)nil;
- if ((imageRep != repNum) || (drawWithAlpha != useAlpha)) {
- id repId;
- if (imageRep < 0) {
- repId = [imageId bestRepresentation];
- imageRep = [[imageId representationList] indexOf:repId];
- } else {
- repId = [[imageId representationList] objectAt:repNum];
- imageRep = repNum;
- }
- if (!repId) return (id)nil;
- backgroundGray = ([repId isKindOf:[NXEPSImageRep class]])? NX_WHITE : NX_LTGRAY;
- drawWithAlpha = useAlpha;
- }
- return self;
- }
-
- // -------------------------------------------------------------------------------------
- // draw methods
-
- /* draw self */
- - drawSelf:(const NXRect *)r :(int)c
- {
- NXRect rect;
-
- /* draw background */
- PSsetgray((imageId)?backgroundGray:NX_LTGRAY);
- NXRectFill(&bounds);
- if (!imageId) return self;
-
- /* center image */
- [imageId getSize:&rect.size];
- rect.X = floor((bounds.W - rect.W) / 2.0);
- rect.Y = floor((bounds.H - rect.H) / 2.0);
-
- /* get representation to draw */
- if (imageRep < 0) [self setImageRep:-1 withAlpha:YES];
- id rep = [[imageId representationList] objectAt:imageRep];
-
- /* draw image */
- if (drawWithAlpha && (NXDrawingStatus == NX_DRAWING)) {
- id tempImage = [[NXImage alloc] initSize:&rect.size];
- [tempImage setFlipped:[imageId isFlipped]];
- PSgsave();
- if ([tempImage lockFocus]) {
- NXRect r = rect;
- r.X = r.Y = 0.0;
- [rep drawIn:&r];
- [tempImage unlockFocus];
- }
- PSgrestore();
- [tempImage composite:NX_SOVER toPoint:&rect.origin];
- [tempImage free];
- } else {
- [rep drawIn:&rect];
- }
-
- return self;
- }
-
- // -------------------------------------------------------------------------------------
- // mouse down
-
- /* mouse down events */
- - mouseDown:(NXEvent*)e
- {
- NXEvent eventCopy = *e;
- NXPoint mousePt = e->location;
-
- /* check for no image */
- if (!imageId) return self;
-
- /* set up image dragging */
- NXRect rect;
- [imageId getSize:&rect.size];
- rect.X = floor((bounds.W - rect.W) / 2.0);
- rect.Y = floor((bounds.H - rect.H) / 2.0);
- [self convertPoint:&mousePt fromView:(id)nil];
- if ((rect.X > 0.0) && (rect.Y > 0.0) && [self mouse:&mousePt inRect:&rect]) {
- NXPoint offset = { 0.0, 0.0 };
- Pasteboard *pboard = [Pasteboard newName:NXDragPboard]; // do not free
- if (imageFile && isSHIFT(NX_ALTERNATEMASK)) {
- [pboard declareTypes:pbTypes num:pbNumTypes owner:(id)nil];
- [pboard writeType:pbTypes[0] data:imageFile length:strlen(imageFile) + 1];
- } else {
- NXAtom dummyType[] = { "_nilType" };
- [pboard declareTypes:dummyType num:1 owner:(id)nil];
- [pboard writeType:dummyType[0] data:"" length:1];
- }
- [self dragImage:imageId at:&(rect.origin) offset:&offset event:&eventCopy
- pasteboard:pboard source:self slideBack:YES];
- } else NXBeep();
-
- return self;
-
- }
-
- /* dragging methods */
- - (NXDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
- {
- if (isLocal || !imageFile) return NX_DragOperationNone;
- return NX_DragOperationGeneric | NX_DragOperationCopy;
- }
-
- @end
-